package cn.choujay.tlias.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

import java.util.Map;

@Component
public class JWTUtil {
    //签名算法
    private final static String KEY_SIGN = "JayChou";
    //有效期
    private final static Long EXPIRE = 43200000L;

    public String genJwt( Map<String, Object> claims) {
        return Jwts.builder().setClaims(claims) //自定义内容(载荷)
                .signWith(SignatureAlgorithm.HS256, KEY_SIGN) //签名算法
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE)) //有效期
                .compact();
    }
    public Claims parseJwt(String jwt) {
        return Jwts.parser().setSigningKey(KEY_SIGN)//指定签名密钥
                .parseClaimsJws(jwt).getBody();
    }

}
